#include "../include/protocol.h"
#include"../include/my_head.h"
#include"../include/mysqlu.h"
#include"../include/command.h"



int randslat(user_t *user)
{
    
    const char* salt_chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    // 初始化随机数种子
    //srand(time(NULL));
    srand(1);
    
    for (size_t i = 0; i < 16; i++) {
        user->salt[i] = salt_chars[rand() % 64];
    }
    user->salt[0] ='$';
    user->salt[1] ='6';
    user->salt[2] ='$';
    user->salt[15] ='$';
    user->salt[16] ='\0';
    printf("salt=%s\n",user->salt);
    return 0;
}


int server_sign_in(user_t* user,int netfd,MYSQL *mysql)
{
    file_t file;
    bzero(&file,sizeof(file_t));
    int sret=0;
    TLVMsg usermsg;
    bzero(user,sizeof(user_t));
    bzero(&usermsg,sizeof(TLVMsg));
    //1.客户端发送name;
    //CMD_SIGN
    sret=recvmasge(netfd,&usermsg);
    if(sret!=0)
    {
        return 0;
    }
    
    strcpy(user->username,usermsg.value);
    printf("msg->usename=%s",usermsg.value);
    printf("user->usernmae=%s",user->username);
   
    
    select_mysql_user(mysql,user,user->username);
    printf("asdfghjkljhgfdsasdfgh---------------------------------------------\n");
    printf("slat=%s",user->salt);

    if(user->salt[0]=='$'){
        sendmasge(netfd,CMD_SIGN,20,"user exit.");
        return -1;
    }else{
        printf("asdfghjkl;------------------------\n");
        //2.服务端发送盐值
        randslat(user);
        printf("salt=%s\n",user->salt);
        sendmasge(netfd,CMD_SIGN,strlen(user->salt),user->salt);
    }
    printf("sadfxgcvnm,mmgfds-----------------------\n");
    //3.客户端发送密文
    bzero(&usermsg,sizeof(TLVMsg));
    sret=recvmasge(netfd,&usermsg);
    if(sret!=0)
    {
        return 0;
    }
    strcpy(user->hashed_password,usermsg.value);
 
    printf("msg   passwd=%s\n",usermsg.value);
    printf("user  passwd=%s\n",user->hashed_password);
    printf("salt=%s\n",user->salt);
    //用户活了
    user->tomb=0;

    //4.插入表中
    int rsqt=mysql_insert_user_mysql(mysql,user);
    printf("rsqt=%d\n",rsqt);
    
    if(rsqt==0)
    {
        strcpy(file.filename, user->username);
       
        strncpy(file.filename, user->username, strlen(user->username));
      
        file.filetype='d';
    
        // strcpy(file.hashfilename,"21321");
        file.parent_id=-1;
        strcpy(file.path,"/");
        strcpy(file.username,user->username);
        file.tombs=user->tomb;
        char * sql[1024];
        snprintf(sql,sizeof(sql),"insert into file(username, parent_id,filename,filetype,path,tomb)values\
        ('%s','%d','%s','%c','%s','%d');",
        file.username,file.parent_id,file.filename,file.filetype,file.path,file.tombs);
        printf("sql=%s\n",sql);
        int qret=mysql_query(mysql,sql);
        return 0;

    }else{
         return -1;
    }
}



int server_login_in(user_t *user,int netfd,MYSQL *mysql)
{
    int sret=0;
    TLVMsg usermsg;
    bzero(user,sizeof(user_t));
    bzero(&usermsg,sizeof(TLVMsg));
    file_t file;
    bzero(&file,sizeof(file_t));


    //1.客户端发送name;
    //CMD_SIGN
    sret=recvmasge(netfd,&usermsg);
    if(sret!=0)
    {
        return 0;
    }
    strcpy(user->username,usermsg.value);
    if(user->username[0]=='用')
    {
        return 0;
    }
    printf("msg->usename=%s\n",usermsg.value);
    printf("user->usernmae=%s\n",user->username);



 
    //1.直接服务取出数据判断是否存在
    select_mysql_user(mysql,user,user->username);
    printf("mysql---user--username=%s,salt=%s,passwd=%s\n",user->username,user->salt,user->hashed_password);
    // if(user->salt[0]=='\0')
    // {
    //     char str_r="数据库不存在用户";
    //     printf("数据库不存在用户\n");
        // sendmasge(netfd,CMD_LOGIN,strlen(str_r),str_r);
        // return 1;
    // }else{  
    printf("--------------------------------------\n");
    //2.服务端发送盐值
    printf("salt=%s\n",user->salt);
    user->salt[15]='$';
    sendmasge(netfd,CMD_LOGIN,strlen(user->salt),user->salt);
    //     printf("--------------------------------------\n");
    //     //send(netfd,user->salt,strlen(user->salt)+1,0);
    // }

   

    //3.客户端发送密文
    bzero(&usermsg,sizeof(TLVMsg));
    sret=recvmasge(netfd,&usermsg);
    if(sret!=0)
    {
        return 0;
    }
    // strcpy(user->hashed_password,usermsg.value);

    printf("pppppppppppppppppppppppp--------------------------------------\n");
    printf("user->hashed_password=%s\n",user->hashed_password);
    printf("msg=%s\n",usermsg.value);
    printf("pppppppppppppppppppppppp--------------------------------------\n");
    //比较密文    
    if(strcmp(usermsg.value,user->hashed_password)==0){
        char buf[]="login in succee";
        sendmasge(netfd, CMD_LOGIN, strlen(buf), buf);
        // PRINT_LOG_FILE(threadpool->logfd, user.username, 8, "login succes", "/", threadpool->mutex);
        return 0;
    }else{
        char bufs[]="login in fiale";
        sendmasge(netfd, 0, strlen(bufs), bufs);
        return -1;
    };
  
}
